package listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JColorChooser;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import view.MainFrame;
import view.MainFrame.Action;
import view.MainFrame.GlassPaneState;
import abstraction.ToolBarButton;
import controller.LayeredPaneController;
import controller.MainController;
import controller.ToolBarController;

/**
 * catch events from toolbar
 * 
 * @author Hugo
 * 
 */
public class ToolBarListener implements ActionListener, MouseListener,
		ChangeListener {
	/**
	 * catch events from toolbar
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() instanceof ToolBarButton) {
			ToolBarButton butSrc = (ToolBarButton) e.getSource();
			if (butSrc.getName().equals("add_empty_layer")) {
				LayeredPaneController.createAnEmptyLayer();
			}
			if (butSrc.getName().equals("add_color_layer")) {
				LayeredPaneController.createAColorLayer();
			}
			if (butSrc.getName().equals("add_img_layer")) {
				LayeredPaneController.createAnImageLayer();
			}
			if (butSrc.getName().equals("add_text_layer")) {
				LayeredPaneController.createATextLayer();
			}
			if (butSrc.getName().equals("move")) {
				ToolBarController.setSelected(butSrc);
				MainFrame.getInstance().setCurrentAction(Action.MOVE);
			}
			if (butSrc.getName().equals("select")) {
				ToolBarController.setSelected(butSrc);
				MainFrame.getInstance().setCurrentAction(Action.SELECT);
			}
			if (butSrc.getName().equals("delete_layer")) {
				LayeredPaneController.removeSelectedLayer();
			}
			if (butSrc.getName().equals("save")) {
				MainController.saveFile();
			}
			if (butSrc.getName().equals("resize")) {
				MainFrame.getInstance().setCurrentAction(Action.SELECT);
				ToolBarController.setSelected((MainFrame.getInstance().getSelectButton()));
				MainFrame.getInstance().setGlassPane(GlassPaneState.RESIZE);
				ToolBarController.glassPaneOn();
			}
			if (butSrc.getName().equals("rotate")) {
				MainFrame.getInstance().setCurrentAction(Action.SELECT);
				ToolBarController.setSelected((MainFrame.getInstance().getSelectButton()));
				MainFrame.getInstance().setGlassPane(GlassPaneState.ROTATE);
				ToolBarController.glassPaneOn();
			}
			if (butSrc.getName().equals("cut")) {
				MainFrame.getInstance().setCurrentAction(Action.SELECT);
				ToolBarController.setSelected((MainFrame.getInstance().getSelectButton()));
                MainFrame.getInstance().setGlassPane(GlassPaneState.CUT);
                ToolBarController.glassPaneOn();
            }
			if (butSrc.getName().equals("erase")) {
                ToolBarController.setSelected(butSrc);
                MainFrame.getInstance().setCurrentAction(Action.ERASE);
            }
			if (butSrc.getName().equals("crayon")) {
				ToolBarController.setSelected(butSrc);
				MainFrame.getInstance().setCurrentAction(Action.DRAW);
			}
			if (butSrc.getName().equals("brush")) {
				ToolBarController.setSelected(butSrc);
				MainFrame.getInstance().setCurrentAction(Action.BRUSH);
			}
			if (butSrc.getName().equals("spray")) {
				ToolBarController.setSelected(butSrc);
				MainFrame.getInstance().setCurrentAction(Action.SPRAY);
			}
			if (butSrc.getName().equals("white")) {
				MainFrame.getInstance().setColorSelected(Color.white);
			}
			if (butSrc.getName().equals("black")) {
				MainFrame.getInstance().setColorSelected(Color.black);
			}
			if (butSrc.getName().equals("green")) {
				MainFrame.getInstance().setColorSelected(Color.green);
			}
			if (butSrc.getName().equals("blue")) {
				MainFrame.getInstance().setColorSelected(Color.blue);
			}
			if (butSrc.getName().equals("red")) {
				MainFrame.getInstance().setColorSelected(Color.red);
			}
			if (butSrc.getName().equals("orange")) {
				MainFrame.getInstance().setColorSelected(Color.orange);
			}
		}

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getSource() instanceof JTextField) {
			JTextField fieldSrc = (JTextField) e.getSource();
			if (fieldSrc.getName().equals("colorSelected")) {
				MainFrame.getInstance().setColorSelected(
						JColorChooser.showDialog(null, "Choose a colour",
								Color.white));
			}
		}
	}

    @Override
    public void stateChanged(ChangeEvent e) {
        if (e.getSource() instanceof JSlider) {
            JSlider sliderSrc = (JSlider) e.getSource();
            MainFrame.getInstance().setSizeSelected(sliderSrc.getValue());
        }
    }

    //----- NOT USED -----

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

	@Override
	public void mousePressed(MouseEvent e) {
	}

	@Override
	public void mouseReleased(MouseEvent e) {
	}
}
